package com.liancheng.smarthome.base;

import android.text.TextUtils;

import com.httpapi.RxSubscriber;
import com.httpapi.apiservice.OnHttpApiListener;
import com.liancheng.smarthome.R;
import com.liancheng.smarthome.utils.common.ToastUtil;

/**
 * 与view搭配的viewmodel
 *
 * 绑定网络请求回调
 *
 */
public abstract class BaseVM<T extends BaseActivity> implements OnHttpApiListener {
    //请求对象，方便绑定到xml进行view启动请求
    protected T context;
    protected RxSubscriber subscriber;

    public BaseVM() {
    }

    /**
     *  绑定view
     * @param context
     */
    public void  bindViewAndModel(T context) {
        this.context = context;
//        model = MvpInstantiationUtils.getInstantiation(this, MvpInstantiationUtils.FIRSTGENERICITY);
//        model.setPresenter(this);
    }

    /**
     * 取消本类中设置的view引用
     */
    public void freeObject() {
        if(subscriber != null){
            subscriber.dispose();
        }
        if(context != null){
            context = null;
        }
    }

    protected void showLoading(){
        if( context != null)
            context.showProcess();
    }
    protected void dissmissLoading(){
        if(context != null)
            context.dismissProcess();
    }
    /**
     * 请求异常提示
     */
    protected void showErrorMsg(){
        if(context == null){
            return;
        }
        context.dismissProcess();
        context.closeRefresh();
        ToastUtil.showShort(context , R.string.str_system_error);
    }

    /**
     * 提示请求失败的提示语:土司方式
     */
    public void showFailMsg(String msg){
        if(context == null){
            return;
        }
        context.dismissProcess();
        context.closeRefresh();
        if(TextUtils.isEmpty(msg)){
            msg = context.getString(R.string.str_system_error);
        }
        ToastUtil.showLong(msg);
    }


}
